package com.wuyou.securitydemo.component;

import com.wuyou.sso.common.contant.CacheKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * CacheUserDetailsServiceImpl
 *
 * @Date 2025/4/10 16:18
 * @Author 无忧
 */
@Service
@Slf4j
public class CacheUserDetailsServiceImpl implements AuthenticationUserDetailsService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
        String key = CacheKey.DEFAULT_USER + token.getName();
        if (!redisTemplate.hasKey(key)) {
            return null;
        }
        return (UserDetails) redisTemplate.opsForValue().get(key);
    }
}
